package com.j2eefast.flowable.bpm.controller;

import com.j2eefast.common.core.business.annotaion.BussinessLog;
import com.j2eefast.common.core.controller.BaseController;
import com.j2eefast.common.core.enums.BusinessType;
import com.j2eefast.common.core.io.file.MimeType;
import com.j2eefast.common.core.utils.FileUploadUtil;
import com.j2eefast.common.core.utils.ResponseData;
import com.j2eefast.common.core.utils.ValidatorUtil;
import com.j2eefast.flowable.bpm.entity.BpmBidLaborSettlementEntity;
import com.j2eefast.flowable.bpm.entity.BpmBidLaborSettlementEntity;
import com.j2eefast.flowable.bpm.entity.BpmProcessDefinitionEntity;
import com.j2eefast.flowable.bpm.entity.BpmTaskFromEntity;
import com.j2eefast.flowable.bpm.service.*;
import com.j2eefast.framework.annotation.RepeatSubmit;
import com.j2eefast.framework.utils.Global;
import com.j2eefast.framework.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 劳务费结算单
 * @author summer
 * @date 2021-06-4 22:19
 */
@Controller
@RequestMapping("/bpm/bidlaborsettlement")
public class BpmBidLaborSettlementController extends BaseController{

    private String prefix = "modules/bpm/bidlaborsettlement";

    /**
     * 必须注入实例关联表单服务
     */
    @Autowired
    private BpmTaskFromService bpmTaskFromService;
    @Autowired
    private BpmBidLaborSettlementService bpmBidLaborSettlementService;
    @Autowired
    private ProcdefService procdefService;
    @Autowired
    private FlowableCommentService flowableCommentService;

    /**
     * 定义关联表单申请表单URL对应此处
     */
    @GetMapping("/add/{id}")
    @RequiresPermissions("bpm:bidlaborsettlement:add")
    public String add(@PathVariable("id") Long id, ModelMap mmp){
        //通过页面传入的表单ID查询表单关联信息
        BpmTaskFromEntity bpmTaskFrom = bpmTaskFromService.getTaskFromById(id);
        mmp.put("bpmTaskFrom", bpmTaskFrom);
        BpmProcessDefinitionEntity proDef = procdefService.getById(bpmTaskFrom.getProcessDefinitionKey());
        mmp.put("proDef", proDef);
        mmp.put("user", UserUtils.getUserInfo());
        return prefix + "/add";
    }

    /**
     * 新增
     */
    @RepeatSubmit
    @BussinessLog(title = "相关文件盖章申请表", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @RequiresPermissions("bpm:bidlaborsettlement:add")
    @ResponseBody
    public ResponseData addForm(@Validated BpmBidLaborSettlementEntity BpmBidLaborSettlementEntity){
        //校验参数
        ValidatorUtil.validateEntity(BpmBidLaborSettlementEntity);
        BpmBidLaborSettlementEntity.setCreateBy(BpmBidLaborSettlementEntity.getLaunchName());
        return bpmBidLaborSettlementService.addForm(BpmBidLaborSettlementEntity);
    }

    /**
     * 新增并悬挂
     */
    @RepeatSubmit
    @BussinessLog(title = "悬挂开标信息登记表", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/saveAsDraft", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("bpm:bidlaborsettlement:add")
    public ResponseData saveAsDraft(@Validated BpmBidLaborSettlementEntity BpmBidLaborSettlementEntity){
        BpmBidLaborSettlementEntity.setCreateBy(BpmBidLaborSettlementEntity.getLaunchName());
        //校验参数

        return bpmBidLaborSettlementService.saveAsDraft(BpmBidLaborSettlementEntity);
    }

    /**
     * 表单详情
     * @param taskId
     * @param businessKey
     * @param mmap
     * @return
     */
    @GetMapping("/view")
    @RequiresPermissions("bpm:bidlaborsettlement:view")
    public String view(@RequestParam(value="taskId", required=true)String taskId,
                       @RequestParam(value="businessKey", required=true)String businessKey,
                       ModelMap mmap){
        BpmBidLaborSettlementEntity BpmBidLaborSettlementEntity = bpmBidLaborSettlementService.getFormById(businessKey);
        mmap.put("bpmForm", BpmBidLaborSettlementEntity);
        mmap.put("taskId", taskId);
        BpmTaskFromEntity bpmTaskFrom = bpmTaskFromService.getById(BpmBidLaborSettlementEntity.getFormId());
        mmap.put("bpmTaskFrom", bpmTaskFrom);
        BpmProcessDefinitionEntity proDef = procdefService.getById(bpmTaskFrom.getProcessDefinitionKey());
        mmap.put("proDef", proDef);
        mmap.put("comment", flowableCommentService.getFlowCommentVosByProcessInstanceId(BpmBidLaborSettlementEntity.getProcessInstanceId()));
        return prefix + "/view";
    }


    @RepeatSubmit
    @BussinessLog(title = "相关文件盖章申请表", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/send", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("bpm:bidlaborsettlement:add")
    public ResponseData sendForm(@RequestParam(value="formDraftId", required=true)String formDraftId){


        return bpmBidLaborSettlementService.sendForm(formDraftId);
    }



    /**
     * 修改
     */
    @GetMapping("/edit")
    @RequiresPermissions("bpm:bidlaborsettlement:add")
    public String edit(@RequestParam(value="taskId", required=true)String taskId,
                       @RequestParam(value="businessKey", required=true)String businessKey,
                       ModelMap mmap){
        BpmBidLaborSettlementEntity bpmOaForm = bpmBidLaborSettlementService.getFormById(businessKey);
        mmap.put("bpmForm", bpmOaForm);
        BpmTaskFromEntity bpmTaskFrom = bpmTaskFromService.getById(bpmOaForm.getFormId());
        mmap.put("bpmTaskFrom", bpmTaskFrom);
        BpmProcessDefinitionEntity proDef = procdefService.getById(bpmTaskFrom.getProcessDefinitionKey());
        mmap.put("proDef", proDef);
        mmap.put("taskId", taskId);
        return prefix + "/edit";
    }

    /**
     * 修改待发流程表单
     */
    @RepeatSubmit
    @RequiresPermissions("bpm:bidlaborsettlement:add")
    @BussinessLog(title = "编辑开标信息登记表", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData editForm(BpmBidLaborSettlementEntity BpmBidLaborSettlementEntity){
        ValidatorUtil.validateEntity(BpmBidLaborSettlementEntity);
        return bpmBidLaborSettlementService.editForm(BpmBidLaborSettlementEntity)? success(): error("修改失败!");
    }

    @GetMapping("/flow")
    @RequiresPermissions("bpm:bidlaborsettlement:add")
    public String flow(@RequestParam(value="taskId", required=true)String taskId,
                       @RequestParam(value="businessKey", required=true)String businessKey,
                       ModelMap mmap){

        BpmBidLaborSettlementEntity bpmOaForm = bpmBidLaborSettlementService.getFormById(businessKey);
        mmap.put("bpmForm", bpmOaForm);
        mmap.put("taskId", taskId);
        BpmTaskFromEntity bpmTaskFrom = bpmTaskFromService.getById(bpmOaForm.getFormId());
        mmap.put("bpmTaskFrom", bpmTaskFrom);
        BpmProcessDefinitionEntity proDef = procdefService.getById(bpmTaskFrom.getProcessDefinitionKey());
        mmap.put("proDef", proDef);
        mmap.put("comment", flowableCommentService.getFlowCommentVosByProcessInstanceId(bpmOaForm.getProcessInstanceId()));
        return prefix + "/flow";
    }



}